package com.example.util;

import com.example.serivce.CommonConstants;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * <p>
 * 类名信息
 * </p>
 *
 * @Author haojieshi
 * @Date 2024/4/24 15:15
 * @Version 1.0
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class ResultMsg<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;

    public static <T> ResultMsg<T> ok() {
        return restResult(null, CommonConstants.SUCCESS, null);
    }

    public static <T> ResultMsg<T> ok(T data) {
        return restResult(data, CommonConstants.SUCCESS, null);
    }

    public static <T> ResultMsg<T> ok(T data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> ResultMsg<T> failed() {
        return restResult(null, CommonConstants.FAIL, null);
    }

    public static <T> ResultMsg<T> failed(String msg) {
        return restResult(null, CommonConstants.FAIL, msg);
    }

    public static <T> ResultMsg<T> failed(T data) {
        return restResult(data, CommonConstants.FAIL, null);
    }

    public static <T> ResultMsg<T> failed(T data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }

    public static <T> ResultMsg<T> failed(int code) {
        return restResult(null, code, null);
    }

    public static <T> ResultMsg<T> failed(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> ResultMsg<T> failed(int code, T data) {
        return restResult(data, code, null);
    }

    public static <T> ResultMsg<T> failed(int code, T data, String msg) {
        return restResult(data, code, msg);
    }

    private static <T> ResultMsg<T> restResult(T data, int code, String msg) {
        ResultMsg<T> apiResult = new ResultMsg<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}